package com.qidianit.mes.md.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.md.mapper.BOMMapper;
import com.qidianit.mes.md.mapper.MaterialMapper;
import com.qidianit.mes.md.service.BOMService;
import com.qidianit.mes.pojo.BOM;
import com.qidianit.mes.pojo.Material;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 奇幻夜之梦
 * @Company 起点编程
 * @Description
 * @Time 2024/10/22 18:31
 */
@Service
public class BOMServiceImpl implements BOMService {
    @Autowired
    private BOMMapper bomMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Override
    public MESResult selectByPage(Long materialId, Integer page, Integer limit) {
        //开启分页  参数1： 页数  参数2： 每页大小
        PageHelper.startPage(page,limit);
        //查询全部
        List<BOM> boms = bomMapper.selectByPage(materialId);
        //创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<BOM> pageInfo = new PageInfo<>(boms);
        // 将pageInfo对象设置到QdResult中
        return MESResult.success(pageInfo);// 返回结果
    }

    @Override
    public MESResult save(Long materialOfId, Long[] materialIds) {
        for (Long materialId : materialIds) {
            Material material = materialMapper.selectById(materialId);
            bomMapper.save(materialOfId,material);
        }
        return MESResult.success();
    }

    @Override
    public MESResult updateBom(BOM bom) {
        return bomMapper.updateById(bom) == 1?MESResult.success():MESResult.error();
    }

    @Override
    public MESResult selectById(Long bomId) {
        return MESResult.success(bomMapper.selectById(bomId));
    }

    @Override
    public MESResult delete(Long bomId) {
        // 修改状态
        return MESResult.isSuccess(bomMapper.deleteById(bomId));
    }

    @Override
    public MESResult deletes(String[] ids) {
        return MESResult.success(Constant.RESPONSE_STATUS_OK,"OK",bomMapper.updateBomsByIds(ids,Constant.DATA_STATUS_DELETE));
    }
}

